<template>
  <div class="robot-clue-trans-log">
    <!-- 搜索工作栏 -->
    <div class="search-container">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" class="search-form">
        <div class="form-row">
          <el-form-item label="创建时间" prop="createTime" class="form-item">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="custom-date-picker"
            />
          </el-form-item>
        </div>

        <div class="button-group">
          <el-button @click="handleQuery" class="search-btn">
            <Icon icon="ep:search" class="btn-icon" />
            搜索
          </el-button>
          <el-button @click="resetQuery" class="reset-btn">
            <Icon icon="ep:refresh" class="btn-icon" />
            重置
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 列表 -->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        border
        class="custom-table"
      >
        <el-table-column label="线索量" align="center" prop="clueTotal" :min-width="120" />
        <el-table-column label="转入量" align="center" prop="transTotal" :min-width="120" />
        <el-table-column
          label="上次查询时间"
          align="center"
          prop="lastQueryTime"
          :formatter="dateFormatter"
          :min-width="160"
        />
        <el-table-column
          label="下次查询时间"
          align="center"
          prop="nextQueryTime"
          :formatter="dateFormatter"
          :min-width="160"
        />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          :min-width="160"
        />
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import * as RobotClueTransLogApi from '@/api/crm/robotcluetranslog'
import { dateFormatter } from '@/utils/formatTime'

defineOptions({ name: 'RobotClueTransLog' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const props = defineProps<{
  ruleId: Number
}>()

watch(
  () => props.moduleId,
  (newVal) => {
    queryParams.ruleId = newVal
    getConfig()
  }
)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  ruleId: props.ruleId,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RobotClueTransLogApi.getClueTransLogPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
.robot-clue-trans-log {
  padding: 16px;
  background: #f8f9fa;
}

/* 搜索容器 */
.search-container {
  background: #ffffff;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.search-form {
  margin: 0;
}

.form-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 16px;
}

.form-item {
  margin-bottom: 0;
  flex: 1;
  min-width: 280px;
}

.custom-date-picker {
  width: 100%;
  min-width: 240px;
}

.custom-date-picker :deep(.el-input__wrapper.is-focus) {
  border-color: #0052cc;
  box-shadow: 0 0 0 1px #0052cc inset;
}

.custom-date-picker :deep(.el-range-separator) {
  color: #0052cc;
}

.custom-date-picker :deep(.el-date-editor .el-range__icon) {
  color: #0052cc;
}

/* 按钮组 */
.button-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: flex-start;
}

.search-btn,
.reset-btn {
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.search-btn {
  background: #0052cc;
  border-color: #0052cc;
  color: white;
}

.search-btn:hover {
  background: #003d99;
  border-color: #003d99;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
}

.reset-btn {
  background: #f5f7fa;
  border-color: #dcdfe6;
  color: #606266;
}

.reset-btn:hover {
  background: #ecf5ff;
  border-color: #0052cc;
  color: #0052cc;
}

.btn-icon {
  margin-right: 4px;
}

/* 表格容器 */
.table-container {
  background: #ffffff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: auto;
}

.custom-table {
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 16px;
}

.custom-table :deep(.el-table__header) {
  background: #f5f7fa;
}

.custom-table :deep(.el-table__header th) {
  background: #f5f7fa;
  border-bottom: 2px solid #0052cc;
  color: #303133;
  font-weight: 600;
  padding: 12px 0;
}

.custom-table :deep(.el-table__body tr:hover > td) {
  background-color: #f0f7ff;
}

.custom-table :deep(.el-table__row--striped td) {
  background: #fafbfc;
}

.custom-table :deep(.el-table__body td) {
  padding: 12px 0;
  color: #303133;
}

.custom-table :deep(.el-table__empty-text) {
  color: #909399;
}

/* 分页容器 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding-top: 16px;
  border-top: 1px solid #e4e7ed;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .robot-clue-trans-log {
    padding: 12px;
  }

  .search-container {
    padding: 16px;
    margin-bottom: 12px;
  }

  .form-row {
    flex-direction: column;
    gap: 12px;
  }

  .form-item {
    min-width: auto;
    width: 100%;
  }

  .custom-date-picker {
    min-width: auto;
  }

  .button-group {
    justify-content: center;
    gap: 8px;
  }

  .search-btn,
  .reset-btn {
    flex: 1;
    min-width: 80px;
  }

  .table-container {
    padding: 12px;
    overflow-x: auto;
  }

  .custom-table {
    min-width: 700px;
  }

  .pagination-container {
    padding-top: 12px;
  }
}

@media (max-width: 480px) {
  .robot-clue-trans-log {
    padding: 8px;
  }

  .search-container {
    padding: 12px;
  }

  .table-container {
    padding: 8px;
  }

  .custom-table {
    min-width: 800px;
  }

  .button-group {
    flex-direction: column;
  }

  .search-btn,
  .reset-btn {
    width: 100%;
  }
}

/* 加载状态优化 */
.el-loading-mask {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);
}

/* 滚动条美化 */
.table-container::-webkit-scrollbar {
  height: 6px;
  width: 6px;
}

.table-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.table-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.table-container::-webkit-scrollbar-thumb:hover {
  background: #0052cc;
}

/* 动画效果 */
.search-container,
.table-container {
  transition: all 0.3s ease;
}

.search-container:hover,
.table-container:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

/* 主题色变量 */
:root {
  --primary-color: #0052cc;
  --primary-hover: #003d99;
  --primary-light: #f0f7ff;
  --border-color: #e4e7ed;
  --text-color: #303133;
  --bg-color: #f8f9fa;
}

/* Element Plus 组件样式覆盖 */
:deep(.el-date-editor.el-input) {
  border-radius: 6px;
}

:deep(.el-date-editor.el-input.is-active) {
  border-color: #0052cc;
}

:deep(.el-picker-panel) {
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

:deep(.el-picker-panel__header) {
  background: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
}

:deep(.el-date-table td.today .el-date-table-cell__text) {
  color: #0052cc;
  font-weight: 600;
}

:deep(.el-date-table td.in-range .el-date-table-cell) {
  background-color: #f0f7ff;
}

:deep(.el-date-table td.start-date .el-date-table-cell),
:deep(.el-date-table td.end-date .el-date-table-cell) {
  background-color: #0052cc;
  color: white;
}

:deep(.el-pagination .btn-next),
:deep(.el-pagination .btn-prev) {
  color: #0052cc;
}

:deep(.el-pagination .el-pager li.is-active) {
  background-color: #0052cc;
  color: #fff;
}

:deep(.el-pagination .el-pager li:hover) {
  color: #0052cc;
}

:deep(.el-pagination .el-select .el-input .el-input__wrapper.is-focus) {
  border-color: #0052cc;
  box-shadow: 0 0 0 1px #0052cc inset;
}

/* 表格数据样式优化 */
.custom-table :deep(.el-table__body td) {
  font-size: 14px;
  line-height: 1.5;
}

.custom-table :deep(.el-table__header th .cell) {
  font-weight: 600;
  color: #303133;
}

/* 空状态样式 */
.custom-table :deep(.el-table__empty-block) {
  padding: 40px 0;
}

.custom-table :deep(.el-table__empty-text) {
  font-size: 14px;
  color: #909399;
}

/* 表格边框优化 */
.custom-table :deep(.el-table--border) {
  border: 1px solid #e4e7ed;
}

.custom-table :deep(.el-table--border::after) {
  background-color: #e4e7ed;
}

.custom-table :deep(.el-table--border .el-table__cell) {
  border-right: 1px solid #e4e7ed;
}

/* 加载动画优化 */
:deep(.el-loading-spinner) {
  color: #0052cc;
}

:deep(.el-loading-text) {
  color: #0052cc;
  font-weight: 500;
}
</style>
